package com.qf.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.qf.homeproject.R;

public class FirstPageListAdapter extends BaseAdapter
{

	final private Context context;
	private List<Map<String, Object>> listViewDatas = new ArrayList<Map<String, Object>>();
	RequestQueue queue;

	public FirstPageListAdapter(Context context,
			List<Map<String, Object>> listViewDatas, RequestQueue queue)
	{
		this.context = context;
		this.listViewDatas = listViewDatas;
		this.queue = queue;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return listViewDatas.size();

	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return listViewDatas.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// Log.i("info", "getView方法执行的次数");
		ViewHolder vHolder;
		if (convertView == null)
		{
			vHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_listview_shouye, null);
			vHolder.img = (ImageView) convertView
					.findViewById(R.id.item_lview_shouye_ImgId);
			vHolder.title = (TextView) convertView
					.findViewById(R.id.item_lview_shouye_titleId);
			vHolder.info = (TextView) convertView
					.findViewById(R.id.item_lview_shouye_InfoId);
			convertView.setTag(vHolder);
		}
		else
		{
			vHolder = (ViewHolder) convertView.getTag();
		}

		// 设置文本数据
		vHolder.title.setText(listViewDatas.get(position).get("title")
				.toString());
		vHolder.info.setText(listViewDatas.get(position).get("summary")
				.toString());

		// Log.i("info", "新闻的主题为:"
		// + listViewDatas.get(position).get("title").toString());

		// 加载图片
		String imgUrl = listViewDatas.get(position).get("groupthumbnail")
				.toString();
		loadImData(vHolder.img, imgUrl);

		return convertView;

	}

	class ViewHolder
	{
		ImageView img;
		TextView title, info;
	}

	private void loadImData(final ImageView img, final String imgUrl)
	{
		ImageRequest imageRequest = new ImageRequest(imgUrl,
				new Listener<Bitmap>()
				{

					@Override
					public void onResponse(Bitmap response)
					{
						// Log.i("info", "图片的url地址为:" + imgUrl);
						img.setImageBitmap(response);
					}
				}, 100, 100, Config.ALPHA_8, new ErrorListener()
				{

					@Override
					public void onErrorResponse(VolleyError error)
					{
						Toast.makeText(context, "网络请求图片失败,请稍后重试",
								Toast.LENGTH_LONG).show();
					}
				});
		queue.add(imageRequest);
	}
}
